package com.cskaoyan.servlet;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.service.AdminAdminService;
import com.cskaoyan.service.AdminAdminServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.URIUtil;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author：LiuFan
 * @Project：Java50th_first
 * @Package：com.cskaoyan.servlet
 * @Date：2023/5/31 10:03
 */
@WebServlet("/admin/admin/*")
public class AdminAdminServlet extends HttpServlet {
    private AdminAdminService adminAdminService = new AdminAdminServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(), req);
        switch (operation){
            case "/list" :
                list(req,resp);
                break;
            case "/create" :
                create(req,resp);
                break;
            case "/update" :
                update(req,resp);
                break;
            case "/delete" :
                delete(req,resp);
                break;
        }
    }

    @SneakyThrows
    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        String jsonStr = req.getReader().readLine();
        MarketAdmin admin = JacksonUtil.read(jsonStr, MarketAdmin.class);
        BaseRespVo delete = adminAdminService.delete(admin);
        ResponseUtil.responseJson(resp,delete);
    }

    @SneakyThrows
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        String jsonStr = req.getReader().readLine();
        MarketAdmin admin = JacksonUtil.read(jsonStr, MarketAdmin.class);
        BaseRespVo update = adminAdminService.update(admin);
        ResponseUtil.responseJson(resp,update);
    }

    @SneakyThrows
    private void create(HttpServletRequest req, HttpServletResponse resp) {
        String jsonStr = req.getReader().readLine();
        MarketAdmin admin = JacksonUtil.read(jsonStr, MarketAdmin.class);
        BaseRespVo create = adminAdminService.create(admin);
        ResponseUtil.responseJson(resp, create);
    }

    @SneakyThrows
    private void list(HttpServletRequest req, HttpServletResponse resp) {
        String username = StringUtil.isEmpty(req.getParameter("username")) ? null : req.getParameter("username");
        Integer page = StringUtil.isEmpty(req.getParameter("page")) ? null : Integer.parseInt(req.getParameter("page"));
//        Integer limit = Integer.parseInt(request.getParameter("limit"));
        Integer limit = StringUtil.isEmpty(req.getParameter("limit")) ? null : Integer.parseInt(req.getParameter("limit"));
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketAdmin> adminList = adminAdminService.list(username, page, limit, sort, order);
        ResponseUtil.responseOkJsonVo(resp, CommonData.data(adminList));
    }
}
